<?php
/**
 * Created by zhe.wang.
 * Date: 2021/4/14
 * Time: 10:55 上午
 */


namespace App\Services\Aliyun;

use OSS\OssClient;
use OSS\Core\OssException;

class OssService
{

    private $_oss;

    function __construct($isPrivate = "")
    {
        if (empty($isPrivate)) {
            $config = config('common.ali.oss')['public'];
        } else {
            $config = config('common.ali.oss')['private'];
        }
        if (isset($this->_oss)) {
            return $this->_oss;
        }
        $this->_oss = new OssClient(
            $config['access_key'],
            $config['access_secret'],
            $config['endpoint']
        );
        $this->bucket = $config['bucket'];
    }

    /**
     * 使用魔术方法捕获所有未注册方法
     * @param  [type] $method    [调用的方法名]
     * @param  [type] $param_arr [调用这个方法所传递的参数]
     * @return [type]            [description]
     */
    public function __call($method, $param_arr)
    {
        return call_user_func_array([$this->_oss, $method], $param_arr);
    }

    /**
     * 上传本地文件
     *
     * @param string $bucket bucket名称
     * @param string $object object名称
     * @param string $file 本地文件路径
     * @param array $options
     * @return null
     * @throws OssException
     */
    public function uploadFile($object, $file)
    {
        $ossclient = $this->_oss;
        $res = $ossclient->uploadFile($this->bucket, $object, $file);
        return $res;
    }

    /**
     * 私有域图片，批量加签
     * @param $imgs
     * @param bool $json
     * @return array
     */
    public static function dealImgs($imgs, $json = false)
    {

        $result = [];
        if (empty($imgs)) {
            return $result;
        }
        if ($json) {
            $imgs = json_decode($imgs, true);
        }

        foreach ($imgs as $img) {
            $result[] = Get_auth_url($img);
        }
        return $result;
    }
}